package com.example.springbooteasycode.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 百分比测试：
 * 现在假设输入N个数，比如15，37，50，我要计算这三个数的各占总和的百分比，并且百分比保留2位小数，百分比相加要为100%。java怎么写一个方法？
 */
public class PercentTest {

    public static void main(String[] args) {
        // 封装方法1
//        BigDecimal[] numbers = {new BigDecimal("66"), new BigDecimal("37"), new BigDecimal("50")};
//        BigDecimal[] numbers = {new BigDecimal("99")};
//        BigDecimal[] numbers = {new BigDecimal("100"), new BigDecimal("0"), new BigDecimal("0")};
//        BigDecimal[] numbers = {new BigDecimal("0"), new BigDecimal("7"), new BigDecimal("0")};
//        BigDecimal[] numbers = {new BigDecimal("0"), new BigDecimal("7"), new BigDecimal("0")};
//        BigDecimal[] numbers = {new BigDecimal("0"), new BigDecimal("0"), new BigDecimal("0")};
//        calculatePercentages(numbers);


        // 封装方法2
//        List<BigDecimal> numbers = Arrays.asList(new BigDecimal("0"), new BigDecimal("0"), new BigDecimal("0"));
//        List<BigDecimal> numbers = Arrays.asList(new BigDecimal("66"), new BigDecimal("37"), new BigDecimal("50"));
//        List<BigDecimal> numbers = Arrays.asList(new BigDecimal("0"), new BigDecimal("37"), new BigDecimal("0"));
//        List<BigDecimal> numbers = Arrays.asList(new BigDecimal("0"));
//        List<BigDecimal> numbers = Arrays.asList(new BigDecimal("1"));
        List<BigDecimal> numbers = Arrays.asList(new BigDecimal("1"), new BigDecimal("3"), new BigDecimal("6"));

        List<BigDecimal> percentages = calculatePercentages(numbers);
        percentages.forEach(System.out::println);
    }

    public static void calculatePercentages(BigDecimal... numbers) {
        // 如果numbers值都为{0,0,0}，则每个数组返回对应的值为{0,0,0}
        if (Arrays.stream(numbers).allMatch(number -> number.compareTo(BigDecimal.ZERO) == 0)) {
            Arrays.stream(numbers).forEach(number -> System.out.println(number));
            return;
        }

        BigDecimal total = Arrays.stream(numbers).reduce(BigDecimal.ZERO, BigDecimal::add);
        for (BigDecimal number : numbers) {
            BigDecimal percentage = number.divide(total, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
            System.out.println(percentage);
        }
    }

    public static List<BigDecimal> calculatePercentages(List<BigDecimal> numbers) {
        if (numbers == null || numbers.isEmpty()) {
            throw new IllegalArgumentException("Input list cannot be empty or null.");
        }

        BigDecimal total = numbers.stream().reduce(BigDecimal.ZERO, BigDecimal::add);

        if (total.equals(BigDecimal.ZERO)) {
            return numbers; // 返回原列表，因为所有数值均为0
        }

        List<BigDecimal> percentages = new ArrayList<>();
        for (BigDecimal number : numbers) {
            BigDecimal percentage = number.divide(total, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
            percentages.add(percentage);
        }

        return percentages;
    }
}
